package com.qm.console.pubdef.jw.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qm.console.model.SpiderArticleDOWithBLOBs;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.pubdef.jw.vo
 * Created on 2021/8/23.
 */
@Getter
@Setter
public class SpiderArticleDTO {

   private Long id;

   private String accountId;

   private Long bindRobotId;

   private Integer sex;

   private String nickname;

   private String signature;

   private String avater;

   private String articleId;

   private Integer type;

   private String subType;

   private String voiceUrls;

   private JSONObject videoUrls;

   private Integer repostCount;

   private Integer commentCount;

   private Integer likeCount;

   private Integer readedCount;

   private Integer hotCount;

   private String duration;

   private Date publishTime;

   private Integer status;

   private Date createTime;

   private Date updateTime;

   private List<String> label;

   private List<String> topicId;

   private String title;

   private String content;

   private String face;

   private JSONArray images;

   public SpiderArticleDOWithBLOBs toDo() {
      SpiderArticleDOWithBLOBs result = new SpiderArticleDOWithBLOBs();
      BeanUtils.copyProperties(this, result);
      if (images != null) {
         result.setImages(JSON.toJSONString(images));
      }
      if (label != null) {
         result.setLabel(JSON.toJSONString(label));
      }
      if (videoUrls != null) {
         result.setVideoUrls(JSON.toJSONString(videoUrls));
      }
      if (topicId != null) {
         result.setTopicId(String.join(",", topicId));
      }
      return result;
   }

   public UserTwitterDTO toUserTwitter() {
      UserTwitterDTO result = new UserTwitterDTO();
      result.setContent(content);
      if (images != null) {
         result.setPictures(JSON.toJSONString(images));
      }
      if (topicId != null) {
         result.setTopicIds(String.join( "," ,topicId));
      }
      if (videoUrls != null) {
         result.setVideo(JSON.toJSONString(videoUrls));
      }
      result.setTwitterType("2");
      result.setTitle(title);
      result.setUid(bindRobotId.intValue());
      result.setCreateTime(publishTime.getTime());
      return result;
   }

   public static SpiderArticleDTO fromDo(SpiderArticleDOWithBLOBs src) {
      SpiderArticleDTO result = new SpiderArticleDTO();
      BeanUtils.copyProperties(src, result);
      if (src.getImages() != null) {
         result.setImages(JSONArray.parseArray(src.getImages()));
      }
      if (src.getLabel() != null) {
         result.setLabel(new ArrayList<>(JSON.parseArray(src.getLabel(), String.class)));
      }
      if (src.getVideoUrls() != null) {
         result.setVideoUrls(JSONObject.parseObject(src.getVideoUrls()));
      }
      if (src.getTopicId() != null) {
         result.setTopicId(Arrays.stream(src.getTopicId().split(",")).collect(Collectors.toList()));
      }
      return result;
   }


}
